package cn.tedu.ymg.admin.controller;


import cn.tedu.ymg.admin.pojo.param.AdminAddNewParam;
import cn.tedu.ymg.admin.pojo.param.AdminLoginInfoParam;
import cn.tedu.ymg.admin.pojo.param.AdminUpdateInfoParam;
import cn.tedu.ymg.admin.pojo.vo.AdminListItemVO;
import cn.tedu.ymg.admin.pojo.vo.AdminStandardVO;
import cn.tedu.ymg.admin.service.IAdminService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import cn.tedu.ymg.commons.security.LoginPrincipal;
import springfox.documentation.annotations.ApiIgnore;
import cn.tedu.ymg.commons.pojo.vo.PageData;
import cn.tedu.ymg.commons.web.JsonResult;

import javax.validation.Valid;

@Slf4j
@RequestMapping("/admin")
@RestController
@Validated
@Api(tags = "01.管理员管理模块")
public class AdminController {
    @Autowired
    IAdminService adminService;

    @PostMapping("/login")
    @ApiOperation("管理员登录")
    @ApiOperationSupport(order = 50)
    public JsonResult login(AdminLoginInfoParam adminLoginInfoParam) {
        log.debug("开始处理【管理员登录】的请求，参数：{}", adminLoginInfoParam);
        String jwt = adminService.login(adminLoginInfoParam);
        return JsonResult.ok(jwt);
    }

    @PostMapping("/add-new")
    @ApiOperation("添加管理员")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AdminAddNewParam adminAddNewParam){
        log.debug("开始处理【添加管理员】的请求，参数：{}", adminAddNewParam);
        adminService.addNew(adminAddNewParam);
        return JsonResult.ok();
    }

    @PostMapping("/delete")
    @ApiOperation("删除管理员")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name="Id", value = "管理员Id", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1,message = "根据ID删除管理员失败，请提交合法的ID值！")
                             @RequestParam Long id) {
        log.debug("开始处理【根据ID删除管理员】的请求，参数：{}", id);
        adminService.delete(id);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改管理员")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "管理员ID",required = true,dataType = "long")
    })
    public JsonResult update(@Range(min = 1,message = "修改管理员失败，请提交合法的ID值！")@RequestParam Long id,
                             @Valid AdminUpdateInfoParam adminUpdateInfoParam) {
        log.debug("开始处理【修改管理员】的请求，参数：{}",adminUpdateInfoParam);
        adminService.updateInfoById(id,adminUpdateInfoParam);
        return JsonResult.ok();
    }

    @GetMapping("/standard")
    @ApiOperation("根据ID查询管理员详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "管理员ID",required = true,dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1,message = "根据ID查询管理员详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询管理员详情】的请求，参数：{}", id);
        AdminStandardVO result = adminService.getStandardById(id);
        return JsonResult.ok(result);
    }

    @GetMapping("/list")
    @ApiOperation("查询管理员列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页码",paramType = "query")
    })
    public JsonResult list(@Range(min = 1,message = "查询列表失败，请提供正确的页码值！")Integer page,
                           @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【查询管理员】的请求，参数：{}",page);
        log.debug("当事人：{}", loginPrincipal);
        if (page == null || page < 1) {
            page = 1;
        }
        PageData<AdminListItemVO> pageData = adminService.list(page);
        return JsonResult.ok(pageData);
    }
}
